<?php

namespace App\Admin\Http\Controllers\System;

use Flash;
use App\Admin\Eloquent\Code;
use App\Admin\Eloquent\CodeType;
use App\Admin\Http\Requests\CodeRequest;
use App\Admin\Http\Controllers\Controller;
use App\Extensions\Pagination\BootstrapThreePresenter;
use \Illuminate\Http\Request;

class CodesController extends Controller
{
	/**
	 * 查询代码信息
	 * 
	 * @param  mixed $typeId
	 * @return \Illuminate\Http\Response
	 */
	public function index($typeId, Request $request)
	{
		$query = $request->only('search');

		$codeType = $this->codeType($typeId);

		$codes = $codeType->paginateQueryCodes($query);

	    if (!empty($query)) {
	      $codes->appends($query);
	    }

		$links = (new BootstrapThreePresenter($codes))->render();

		return view('admin.system.codes.index', compact('codeType', 'codes', 'links', 'query'));
	}

	/**
	 * 查看代码信息
	 * 
	 * @param  mixed $typeId
	 * @return \Illuminate\Http\Response
	 */
	public function show($typeId, $id)
	{
		$codeType = $this->codeType($typeId);

		$code = $this->code($id);

		return view('admin.system.codes.show', compact('codeType', 'code'));
	}

	/**
	 * 新增代码信息表单
	 * 
	 * @param  mixed $typeId
	 * @return \Illuminate\Http\Response
	 */
	public function create($typeId)
	{
		$codeType = $this->codeType($typeId);

		$parents = $codeType->codes;

		return view('admin.system.codes.create', compact('codeType', 'parents'));
	}

    /**
     * 新增代码信息
     * 
     * @param  mixed  $typeId
     * @param  \App\Admin\Http\Requests\CodeRequest $request
     * @return \Illuminate\Http\Response
     *
     * @throws \App\Exceptions\BusinessException
     */
	public function store($typeId, CodeRequest $request)
	{
		$codeType = $this->codeType($typeId);

		$code = new Code($request->input());

		$code->type_id = $codeType->id;

		$code->save();

		Flash::success('新增代码信息成功');

		return $this->redirectAction('System\CodesController@show', [$codeType, $code]);
	}

	/**
	 * 编辑代码信息表单
	 * 
	 * @param  mixed $typeId
	 * @return \Illuminate\Http\Response
	 */
	public function edit($typeId, $id)
	{
		$codeType = $this->codeType($typeId);

		$code = $this->code($id);

		return view('admin.system.codes.edit', compact('codeType', 'code'));
	}

    /**
     * 编辑代码信息
     * 
     * @param  mixed  $typeId
     * @param  mixed  $id
     * @param  \App\Admin\Http\Requests\CodeRequest $request
     * @return \Illuminate\Http\Response
     *
     * @throws \App\Exceptions\BusinessException
     */
	public function update($typeId, $id, CodeRequest $request)
	{
		$codeType = $this->codeType($typeId);

		$code = $this->code($id);

		$code->update($request->input());

		Flash::success('编辑代码信息成功');

		return $this->redirectAction('System\CodesController@show', [$codeType, $code]);
	}

	/**
   * 删除代码
   *
   * @param  mixed $codeTypeId
   * @param  mixed $codeId
   * @return \Illuminate\Http\Response
   */
  public function destroy($typeId, $id)
  {
    $codeType = $this->codeType($typeId);

    $code = $this->code($id);

    $code->delete();

    Flash::success('删除代码成功');

    return $this->redirectAction('System\CodesController@index', [$codeType]);
  }

	/**
	 * 代码类别
	 * 
	 * @param  mixed $id [description]
	 * @return \App\Admin\Eloquent\CodeType
	 *
	 * @throws \Illuminate\Database\Eloquent\ModelNotFoundException
	 */
	protected function codeType($id)
	{
		return CodeType::findOrFail($id);
	}

	/**
	 * 代码
	 * 
	 * @param  mixed $id
	 * @return \App\Admin\Eloquent\Code
	 * 
	 * @throws \Illuminate\Database\Eloquent\ModelNotFoundException 
	 */
	protected function code($id)
	{
		return Code::findOrFail($id);
	}
}
